package com.ycy.callactivity;

import com.ycy.BaseNoListenerTest;
import com.ycy.BaseTest;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CreateDemo extends BaseTest {

  @Test
  public void creatProcDef() {
    // 部署流程
    RepositoryService repositoryService = processEngine.getRepositoryService();
    Deployment deployment = repositoryService.createDeployment()
//            .addClasspathResource("callactivity/sub.bpmn20.xml")
            .addClasspathResource("callactivity/callactivity.bpmn20.xml")
            //subprocess
            .deploy();
    System.out.println(deployment);
  }

  @Test
  public void createProcInst() {
    RuntimeService runtimeService = processEngine.getRuntimeService();
    HashMap<String, Object> variables = new HashMap<>();
    List<Long> zhixingrens = new ArrayList<>();
    zhixingrens.add(1L);
    zhixingrens.add(2L);
    variables.put("assigneeList", zhixingrens);

    ProcessInstance processInstance =
            runtimeService.startProcessInstanceByKey("test_subProcess", variables);
    String processInstanceId = processInstance.getProcessInstanceId();
    System.out.println(processInstanceId);
  }

  /**
   * 查询流程实例下的所有任务
   */
  @Test
  public void queryProcessInstanceIdTask() {
    TaskService taskService = processEngine.getTaskService();
    //获取流程实例中的task
    List<Task> list = taskService.createTaskQuery().processInstanceId("52516").list();
    for (Task task : list) {
      System.out.println(task + " " + task.getProcessInstanceId());
      //查询所有参数
      Map<String, Object> variables = taskService.getVariables(task.getId());
      System.out.println(variables);
    }
  }

  /**
   * 11. 历史
   * HistoricActivityInstance保存活动（流程中的节点）的一次执行的信息
   */
  @Test
  public void queryActivity() {
    HistoryService historyService = processEngine.getHistoryService();
    List<HistoricActivityInstance> activities =
            historyService.createHistoricActivityInstanceQuery().activityType("callActivity")
                    .processInstanceId("50001")
                    .list();
    for (HistoricActivityInstance activity : activities) {
      activity.getActivityId();
      System.out.println(activity.getId()+ " " +  activity.getActivityName() + " " +
              activity.getStartTime() + " " + activity.getEndTime());
    }
  }

  /*
  审批 通过
   */
  @Test
  public void passParent() {
    TaskService taskService = processEngine.getTaskService();
    //查询
    List<Task> tasks = taskService.createTaskQuery().processInstanceId("50001").list();
    System.out.println("You have " + tasks.size() + " tasks:");

    Map<String, Object> variables = new HashMap<String, Object>();
    //定义子流程
    ArrayList<String> skiping = new ArrayList<String>();
    skiping.add("subprocess");
    variables.put("subs", skiping);
    ArrayList<Long> assignee = new ArrayList<Long>();
    assignee.add(1L);
    assignee.add(2L);
    variables.put("ASSIGNEE_zhixingren1_LIST", assignee);

    Task task = tasks.get(0);
    taskService.complete(task.getId(), variables);
  }

  @Test
  public void passSub(){
    TaskService taskService = processEngine.getTaskService();
    taskService.complete("52541");
  }

  //查询流程下的所有任务
  @Test
  public void querySubTask() {
    TaskService taskService = processEngine.getTaskService();
    //获取流程实例中的task
    List<Task> list = taskService.createTaskQuery().processDefinitionKey("skiping").list();
    for (Task task : list) {
      System.out.println(task);
      //查询所有参数
      Map<String, Object> variables = taskService.getVariables(task.getId());
      System.out.println(variables);
    }
  }

}
